package thinkinginjava.generics;

public class LinkedStack5 <T>{
	private class Node {// define node
		T item;
		Node next;
		Node() {
			this.item = null;
			this.next = null;
		}
		Node(T item, Node next) {
			this.item = item;
			this.next = next;
		}
		boolean end() {
			return this.item == null && this.next == null;
		}
	}
	
	private Node top = new Node();// end sentinel
	public void push(T item) {// push
		top = new Node(item, top);
	}
	public T pop() {// pop
		T result = top.item;
		if(!top.end()) {
			top = top.next;
		}
		return result;
	}
	
	// test
	public static void main(String[] args) {
		LinkedStack5<String> lss = new LinkedStack5<String>();
		for (String s : "Phasers on stun!".split(" ")) {
			lss.push(s);
		}
		String s;
		while((s = lss.pop()) != null) {
			System.out.println(s);
		}
	}
}
